package com.chis.ms;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Tt {

//    private static String fhdh = "RK50010900022021011902,RK50024100022021012002,RK50015500022021012101,RK50010800022021012108,RK50010600022021012202,RK50023800022021012501,RK50010500022021012804,RK50010600022021012901,RK50024000022021012901,RK50023100022021012901,RK50011500022021020301,RK50010200022021020301,RK50011200022021020405,RK50011200022021020405,RK50011800022021020503,RK50023100022021020701,RK50023500022021022401,RK50010300022021022501,RK50015500022021022601,RK50010500022021022607,RK50015200022021030502,RK50010400022021030903,RK50011500022021031101,RK50023100022021031202,RK50010800022021031505,RK50011400022021031701,RK50010900022021031801,RK50023000022021032204,RK50023300022021032201,RK50011700022021032601,RK50010500022021033009,RK50010600022021033005,RK50010600022021033005,RK50011300022021033002,RK50011300022021033002,RK50011300022021042103,RK50011100022021042501,RK50015300022021042501,RK50023100022021042901,RK50010500022021043002,RK50010800022021043001,RK50011300022021043001,RK50015100022021050703,RK50010700022021051102,RK50015300022021051201,RK50011100022021051201,RK50010200022021051203,RK50010800022021052101,RK50010900022021052101,RK50011200022021052102,RK50010500022021052606,RK50015200022021052602,RK50011200022021060305,RK50010700022021060304,RK50015200022021060403,RK50023700022021061101,RK50023700022021061101,RK50010900022021061502,RK50023800022021061103,RK50011900022021061101,RK50010600022021061503,RK50023100022021061604,RK50023300022021061701,RK50023000022021061701,RK50015300022021062203,RK50015500022021062301,RK50015500022021062301,RK50010700022021062305,RK50011100022021062402,RK50011200022021062402,RK50011500022021062505,RK50011700022021062804,RK50011300022021062802,RK50011300022021062802,RK50010300022021062802,RK50010200022021062801,RK50015100022021062901,RK50015100022021062901,RK50023500022021062901,RK50023500022021062901,RK50024000022021062902,RK50011200022021062902,RK50011200022021062902,RK50010600022021062901,RK50011700022021062901,RK50010500022021062912,RK50010500022021062912,RK50010800022021071909,RK50010800022021071909,RK50023600022021072101,RK50010700022021072305,RK50010500022021073003,RK50010300022021073004,RK50011900022021080201,RK50010800022021080201,RK50015200022021081601,RK50015200022021081601,RK50010700022021081604,RK50024100022021082306,RK50010500022021083011,RK50011800022021090205,RK50023100022021090601,RK50010800022021091311,RK50015200022021091401,RK50015100022021091402,RK50010900022021091404,RK50011400022021091503,RK50012000022021091502,RK50015400022021091601,RK50011100022021091706,RK50010700022021091816,RK50015300022021092001,RK50010500022021092807,RK50010600022021092901,RK50011200022021092901,RK50011200022021092901,RK50010700022021092901,RK50023600022021102701,RK50010500022021102901,RK50010500022021102903,RK50024100022021110102,RK50015200022021110201,RK50010300022021110201,RK50010700022021110202,RK50024000022021110301,RK50011800022021110501,RK50010800022021111102,RK50010600022021111503,RK50023100022021111702,RK50023500022021111802,RK50010400022021111902,RK50011100022021112204,RK50023000022021112403,RK50011200022021112902,RK50015300022021113001,RK50011000022021121502,RK50023100022021121701,RK50011300022021121705,RK50010700022021122004,RK50010300022021122005,RK50011400022021122102,RK50015100022021122102,RK50011100022021122103,RK50010900022021122704,RK50024100022021122704,RK50023500022021122801,RK50023600022021122801,RK50010500022021122804,RK50011700022021122904,RK50010200022021123002,RK50010200022021123002,RK50010600022021123001,RK50011200022021123001,RK50011500022021123001,RK50011900022021123002,RK50015200022021123001,RK50011800022021123101,RK50023300022021123102";

    private static String fhdh ="RK50010300022021030202,RK50010300022021042802,RK50010300022021072601,RK50010300022021093001,RK50010300022021122004,RK50010400022021042802,RK50010400022021072701,RK50010400022021091503,RK50010400022021122003,RK50010500022021022605,RK50010500022021033005,RK50010500022021033005,RK50010500022021042901,RK50010500022021052708,RK50010500022021062916,RK50010500022021062918,RK50010500022021072806,RK50010500022021092614,RK50010500022021102807,RK50010500022021112901,RK50010500022021122806,RK50010600022021080604,RK50010600022021091801,RK50010600022021091801,RK50010600022021112602,RK50010700022021123001,RK50010800022021012107,RK50010800022021031804,RK50010800022021052702,RK50010800022021072202,RK50010800022021091401,RK50010800022021111801,RK50010900022021040901,RK50010900022021052002,RK50010900022021060401,RK50010900022021081601,RK50010900022021102801,RK50010900022021122701,RK50010900022021122703,RK50011000022021012901,RK50011000022021111001,RK50011100022021020101,RK50011100022021053101,RK50011100022021053102,RK50011100022021060301,RK50011100022021083003,RK50011100022021112203,RK50011100022021122104,RK50011200022021022502,RK50011200022021033101,RK50011200022021062201,RK50011200022021080202,RK50011200022021092206,RK50011200022021092902,RK50011200022021123002,RK50011300022021040801,RK50011300022021082701,RK50011300022021102101,RK50011300022021122401,RK50011400022021052803,RK50011400022021052804,RK50011400022021060701,RK50011400022021082301,RK50011600022021020502,RK50011600022021120301,RK50011700022021041301,RK50011800022021020104,RK50011800022021040903,RK50011800022021052001,RK50011800022021061802,RK50011800022021072201,RK50011900022021051401,RK50011900022021070801,RK50011900022021072207,RK50011900022021072902,RK50011900022021093002,RK50011900022021123101,RK50012000022021031201,RK50012000022021040901,RK50012000022021082404,RK50012800022021051401,RK50012800022021062201,RK50012800022021072902,RK50012800022021090802,RK50012800022021122402,RK50015100022021020101,RK50015100022021060201,RK50015100022021072203,RK50015100022021090202,RK50015100022021093001,RK50015100022021120202,RK50015200022021060402,RK50015200022021092202,RK50015200022021122108,RK50015300022021020101,RK50015300022021020102,RK50015300022021031902,RK50015300022021091702,RK50015400022021030403,RK50015400022021060904,RK50015400022021080602,RK50015400022021080602,RK50015400022021100801,RK50015400022021122902,RK50015500022021031801,RK50015500022021081801,RK50015600022021031901,RK50015600022021123002,RK50022900022021062402,RK50022900022021091402,RK50022900022021091402,RK50022900022021122901,RK50023000022021033101,RK50023000022021081904,RK50023000022021081905,RK50023000022021123001,RK50023100022021122801,RK50023300022021042001,RK50023300022021072601,RK50023600022021030401,RK50023600022021081901,RK50023600022021092903,RK50023700022021020501,RK50023700022021111803,RK50024100022021020401,RK50024100022021032901,RK50024100022021072803,RK50024100022021122101,RK50024200022021052403,RK50024200022021090901,RK50024200022021111601,RK50024300022021051401,RK50024300022021051401,RK50024300022021081712";
    public static void main(String[] args) throws Exception {
        String mainpath = "C:\\Users\\wlj\\Desktop\\药监狂苗排查\\dd\\2016";
        File file = new File(mainpath);
        File[] files = file.listFiles();

        for (File file2 : files) {
            File[] zipfiles = file2.listFiles();
            for (File file3 : zipfiles) {

                String zippath = file3.getAbsolutePath();
                if(!zippath.endsWith("zip")){
                    continue;
                }
                String xmlpath = file3.getAbsolutePath().replace(file3.getName(),"data");
                try {
                    unzip(zippath,xmlpath);
                } catch (Exception e){
                    System.out.println(zippath);
                    continue;
                }

                String filepath = xmlpath+"\\data.xml";
                File datafile = new File(filepath);
                BufferedReader reader = null;
                try {
                    reader= new BufferedReader(new FileReader(datafile));
                } catch (Exception e){
                    System.out.println(zippath);
                    continue;
                }

                String eventid = null;
                String dh = null;
                String rh = null;
                String ym = null;
                String ph = null;
                String sl = null;
                String star = null;
                while (true) {
                    String nextline = null;
                    try {
                        nextline = reader.readLine();
                        if (nextline != null && nextline.length() > 8) {
                            if(nextline.contains("eventID")){
                                eventid = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("FHDBH")){
                                dh = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("SHDBH")){
                                rh = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("GJYPBSM")){
                                ym = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("YMSCPH")){
                                ph = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("SHSL")){
                                sl = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                            }
                            if(nextline.contains("</instanceList>")){
                                star = "1";
                            }

                            if(star != null && star.equals("1") && fhdh.contains(rh) && (ym.equals("04560000201") || ym.equals("04560000202"))){

                                String dg = eventid+"#"+dh+"#"+rh+"#"+ym+"#"+ph+"#"+sl;

                                System.out.println(dg);

                                star = "0";
                            }

                        }


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (nextline == null) break;
                }


            }
        }

    }

    public static void unzip(String zipFilePath, String desDirectory) throws Exception {

        File desDir = new File(desDirectory);
        if (!desDir.exists()) {
            boolean mkdirSuccess = desDir.mkdir();
            if (!mkdirSuccess) {
                throw new Exception("创建解压目标文件夹失败");
            }
        }
        // 读入流
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
        // 遍历每一个文件
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) { // 文件夹
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                // 直接创建
                mkdir(new File(unzipFilePath));
            } else { // 文件
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                File file = new File(unzipFilePath);
                // 创建父目录
                mkdir(file.getParentFile());
                // 写出文件流
                BufferedOutputStream bufferedOutputStream =
                        new BufferedOutputStream(new FileOutputStream(unzipFilePath));
                byte[] bytes = new byte[1024];
                int readLen;
                while ((readLen = zipInputStream.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, readLen);
                }
                bufferedOutputStream.close();
            }
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }

    private static void mkdir(File file) {
        if (null == file || file.exists()) {
            return;
        }
        mkdir(file.getParentFile());
        file.mkdir();
    }
}
